package com.ruoyi.project.jiaqin.followcompany.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.jiaqin.followcompany.domain.JiaqinFollowCompany;
import com.ruoyi.project.jiaqin.followcompany.mapper.JiaqinFollowCompanyMapper;
import com.ruoyi.project.jiaqin.followcompany.service.IJiaqinFollowCompanyService;
import com.ruoyi.project.system.member.domain.MemberIdentity;
import com.ruoyi.project.system.member.mapper.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 企业关注Service业务层处理
 *
 * @author admin
 * @date 2021-01-04
 */
@Service
public class JiaqinFollowCompanyServiceImpl implements IJiaqinFollowCompanyService
{
    @Autowired
    private JiaqinFollowCompanyMapper jiaqinFollowCompanyMapper;
    //企业表的创建 收藏功能  用户权限
    @Autowired
    private ICompanyService iCompanyService;

    @Autowired
    private MemberMapper memberMapper;

    /**
     * 查询企业关注
     *
     * @param id 企业关注ID
     * @return 企业关注
     */
    @Override
    public JiaqinFollowCompany selectJiaqinFollowCompanyById(String id)
    {
        return jiaqinFollowCompanyMapper.selectJiaqinFollowCompanyById(id);
    }

    @Override
    public JiaqinFollowCompany selectJiaqinFollowMemberByMemberIdAndToCompanyId(String memberId, String toCompanyId) {
        return jiaqinFollowCompanyMapper.selectJiaqinFollowMemberByMemberIdAndToCompanyId(memberId,toCompanyId);
    }

    @Override
    public PageInfo<JiaqinFollowCompany> selectJiaqinFollowCompanyByMemberId(Integer pageNum, Integer pageSize, String memberId) {
        PageHelper.startPage(pageNum,pageSize);
        List<JiaqinFollowCompany> list = jiaqinFollowCompanyMapper.selectJiaqinFollowCompanyByMemberId(memberId);
        if(list != null && list.size()!= 0){
            for (JiaqinFollowCompany j :list) {
                //根据企业用户ID查询是否钻石VIP
                Company company = iCompanyService.selectCompanyById(j.getFollowCompanyId());
                if(company !=null){
                    MemberIdentity mi = memberMapper.selectMemberIdentityById(company.getMemberId());
                    company.setMemberIdentity(mi);
                    // 备注
                    if (!j.getRemarks().equals("0")) {
                        company.setCompanyName(j.getRemarks());
                    }
                    // 距离
//                    Member member1 = memberMapper.selectMemberById(memberId);
//                    if (member1 != null) {
//                        double distance = DistanceUtils.getDistance(member1.getLongitude().doubleValue(), member1.getLatitude().doubleValue()
//                                , company.getLongitude().doubleValue(), company.getLatitude().doubleValue());
//                        DecimalFormat df = new DecimalFormat("######0.0");
//                        df.format(distance);
//                        j.setDistance(df.format(distance));
//                    } else {
//                        j.setDistance("0.1");
//                    }
                }
                j.setCompany(company);
                j.setTotal(list.size());
            }
        }
        PageInfo<JiaqinFollowCompany> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 查询企业关注列表
     *
     * @param jiaqinFollowCompany 企业关注
     * @return 企业关注
     */
    @Override
    public List<JiaqinFollowCompany> selectJiaqinFollowCompanyList(JiaqinFollowCompany jiaqinFollowCompany)
    {
        return jiaqinFollowCompanyMapper.selectJiaqinFollowCompanyList(jiaqinFollowCompany);
    }

    /**
     * 新增企业关注
     *
     * @param jiaqinFollowCompany 企业关注
     * @return 结果
     */
    @Override
    public int insertJiaqinFollowCompany(JiaqinFollowCompany jiaqinFollowCompany)
    {
        jiaqinFollowCompany.setId(UUID.randomUUID().toString().replace("-",""));
        jiaqinFollowCompany.setRemarks("0");
        return jiaqinFollowCompanyMapper.insertJiaqinFollowCompany(jiaqinFollowCompany);
    }

    /**
     * 修改企业关注
     *
     * @param jiaqinFollowCompany 企业关注
     * @return 结果
     */
    @Override
    public int updateJiaqinFollowCompany(JiaqinFollowCompany jiaqinFollowCompany)
    {
        return jiaqinFollowCompanyMapper.updateJiaqinFollowCompany(jiaqinFollowCompany);
    }

    /**
     * 删除企业关注对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJiaqinFollowCompanyByIds(String ids)
    {
        return jiaqinFollowCompanyMapper.deleteJiaqinFollowCompanyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除企业关注信息
     *
     * @param id 企业关注ID
     * @return 结果
     */
    @Override
    public int deleteJiaqinFollowCompanyById(String id)
    {
        return jiaqinFollowCompanyMapper.deleteJiaqinFollowCompanyById(id);
    }
}
